package com.xunkids.bos.migration.component.log.impl;

import com.alibaba.fastjson2.JSON;
import com.xunkids.bos.migration.component.log.MigrationLog;


import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * ktthings kv log
 *
 * @author baiyanlong
 * @version 1.0.0
 * @since 2022/10/28 14:28
 */
public class KvLog implements MigrationLog {

    /**
     * 类型
     */
    private final String type;
    /**
     * 级别
     */
    private final String level;
    private final String group;
    private final Map<String, Object> kv;
    private final Map<String, String> label;
    private final String timestamp;
    private String msg;


    public String getType() {
        return type;
    }

    public String getLevel() {
        return level;
    }

    public String getGroup() {
        return group;
    }

    public Map<String, Object> getKv() {
        return kv;
    }

    public Map<String, String> getLabel() {
        return label;
    }

    public String getTimestamp() {
        return timestamp;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public KvLog(String group, String type, String level, int kvSize, String msg) {
        this.group = group;
        this.type = type;
        this.level = level;
        this.msg = msg;
        this.kv = new HashMap<>(kvSize);
        this.label = new HashMap<>(kvSize);
        this.timestamp = LocalDateTime.now().toString();
    }

    /**
     * 日志类型
     *
     * @return 日志类型
     */
    @Override
    public String type() {
        return type;
    }

    /**
     * 日志级别
     *
     * @return 日志级别
     */
    @Override
    public String level() {
        return level;
    }

    /**
     * 标签
     *
     * @param name  标签
     * @param value 值
     */
    @Override
    public void label(String name, String value) {
        label.put(name, value);
    }

    /**
     * log group
     *
     * @return 日志组
     */
    @Override
    public String group() {
        return group;
    }

    public void put(String key, Object value) {
        kv.put(key, value);
    }

    /**
     * json log
     *
     * @return json log
     */
    @Override
    public String jsonLog() {
        return JSON.toJSONString(toMap());
    }

    /**
     * map
     *
     * @return json log
     */
    @Override
    public Map<String, Object> toMap() {
        Map<String, Object> data = new HashMap<>();
        data.put("type", type);
        data.put("level", level);
        data.put("kv", kv);
        data.put("label", label);
        data.put("timestamp", timestamp);
        data.put("msg", msg);
        return data;
    }
}
